package com.example.demo.event.guava;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @author lyc
 * @date 2019/6/10.
 * https://iamzhongyong.iteye.com/blog/2036612
 */
@Component
public class EventBusService {
  // private static EventBus eventBus = new EventBus("user"); //同步

    private static AsyncEventBus eventBus = new AsyncEventBus("user", Executors.newFixedThreadPool(10));//异步

    /**
     * 注册事件
     * @param obj
     */
   public static void register(Object obj){
       eventBus.register(obj);
   }

    /**
     * 解除注册
     * @param obj
     */
   public static void unregister(Object obj){
       eventBus.unregister(obj);
   }

    /**
     * 发布事件
     * @param obj
     */
   public static void post(Object obj){
       eventBus.post(obj);
   }
}